﻿using System;
using System.Linq;
using System.Web.Mvc;
using CinemaNetwork.Models;

namespace CinemaNetwork.Controllers
{
    public class AccountController : Controller
    {

        private CinemaEntities db = new CinemaEntities();
        public ActionResult LogOn()
        {

            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult LogOn([Bind] LogOnModel model)
        {
            if (ModelState.IsValid)
            {
                var client = (from user in db.users 
                            where user.Login== model.UserName 
                            select user).FirstOrDefault();
                if (client != null && client.Password == model.Password)
                {



                    if (client.Login == "Admin")
                    {

                        Session["Admin"] = client.Login;
                    }
                    else
                    {
                        Session["User"] = client.Login;
                    }



                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    ModelState.AddModelError("", "Логин или пароль неверны.");
                }
            }
            return View(model);
        }

        public ActionResult Logout()
        {
            if (Session["Admin"] != null)
            {

                Session["Admin"] = null;
            }
            else
            {
                if (Session["User"] != null)
                {

                    Session["User"] = null;
                }
            }

            return RedirectToAction("Index", "Home");
        }

        public ActionResult Register()
        {
            ViewData["PasswordLength"] = 7;
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Register([Bind] RegisterModel model)
        {
           
            if (ModelState.IsValid)
            {
                user client = new user();
                client.Login = model.UserName;
                client.Password = model.Password;

                var result =(from user in db.users 
                            where user.Login== client.Login 
                            select user).Count();
          

                if (model.Password.Equals(model.ConfirmPassword) && result == 0 )
                {
                    Session["User"] = client.Login;

                    db.AddTousers(client);
                    db.SaveChanges();
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    ModelState.AddModelError("", "Произошла ошибка");
                }
            }

            ViewData["PasswordLength"] = 7;
            return View(model);
        }

      




        public ActionResult MyAcc()
        {
            String s  = Session["User"].ToString();

            var reservations = (from MyReserv in db.MyReservs
                                where MyReserv.Login == s
                                select MyReserv).ToList();
            return View(reservations);
        }



        
        public ActionResult AddReserv(int place_id,int session_id)
        {


            var addReserve = (from Reservation in db.Reservations
                              where Reservation.place_id ==  place_id
                              where Reservation.session_id == session_id

                              select Reservation).First();
            String str = Session["User"].ToString();

            var client = (from user in db.users
                          where user.Login == str
                          select user).First();



            try
            {
                UpdateModel(client);

                addReserve.IsReserved = 1;
                addReserve.user_id = client.id;
                
                UpdateModel(addReserve);
                db.SaveChanges();

            }

            catch
            {
                return View(addReserve);
            }
            return RedirectToAction("MyAcc");
        }

        public ActionResult DeleteReserve(int id)
        {

            var delReserve = (from Reservation in db.Reservations
                                where Reservation.id == id

                                select Reservation).First();

            try
            {
                delReserve.IsReserved = 0;
                delReserve.user_id = 1;
                UpdateModel(delReserve);
                db.SaveChanges();

            }

            catch {
                return View();
            }
            return RedirectToAction("MyAcc");
        }
    }
}
